import oss2
import os

access_key_id = 'LTAI5tBwipZW14UMAa9bqWsZ'
access_key_secret = '2cVpECSgQLHAJaJxCUk2kAhS3H9yar'
endpoint = 'oss-cn-hangzhou.aliyuncs.com'
bucket_name = 'gcxsjcn'
object_name = 'ljy'

# 获取OSSClient实例
auth = oss2.Auth(access_key_id, access_key_secret)
bucket = oss2.Bucket(auth, endpoint, bucket_name)


def upload_file_to_oss(filepath):
	# 创建Auth实例
	auth = oss2.Auth(access_key_id, access_key_secret)
	# 创建Bucket实例
	bucket = oss2.Bucket(auth, endpoint, bucket_name)
	# 上传文件
	dirname, filename = os.path.split(filepath)
	# 如果文件名前没有路径，则使用根目录（避免直接上传到根目录，可以设置一个默认前缀）
	if not dirname:
		dirname = '/'  # 或者你想要的默认文件夹名，比如 '/uploads/'
	# 构造完整的对象名（包含文件夹路径）
	object_name = os.path.join(dirname, filename)

	bucket.put_object_from_file(object_name, filepath)

	print(f"文件 {filepath} 上传 {bucket_name}/{object_name} 成功.")


if __name__ == "__main__":
	# 替换成你的OSS配置
	filepath = input("请输入文件路径：")  # 用户输入文件路径

	upload_file_to_oss(filepath)
